<?php

/**
 * 前台分页
 * @param type $p 当前页码
 * @param type $count 数据总条数
 * @param type $page_size 每页显示数量
 * @param type $now_page 每页显示页码数量
 * @return string
 */
function page($p, $count, $page_size, $now_page = 8) {
    $total_page = ceil($count / $page_size);
    $init = 1;
    //页码个数
    $page_num = ($now_page % 2) ? $now_page : $now_page + 1;
    $pageoffset = ($page_num - 1) / 2; //页码个数左右偏移量
    //分页数大于页码个数时可以偏移
    if ($total_page > $page_num) {
        //如果当前页小于等于左偏移
        if ($p <= $pageoffset) {
            $init = 1;
            $max = $page_num;
        } else {
            //如果当前页码右偏移超出最大分页数
            if ($p + $pageoffset >= $total_page + 1) {
                $init = $total_page - $page_num + 1;
                $max = $total_page;
            } else {
                //左右偏移都存在时的计算
                $init = $p - $pageoffset;
                $max = $p + $pageoffset;
            }
        }
    } else {
        $max = $total_page;
    }
    $html = '';
    if ($p == 1) {
        $html.= '<span class="page-prev">Previous</span>';
    } else {
        $prev = $p - 1;
        $html.='<a rel="nofollow" class="page-prev" href="javascript:void(0)" onclick="GetManageProductList(' . $prev . ')">Previous</a>';
    }
    for ($i = $init; $i <= $max; $i++) {
        if ($p == $i) {
            $html.='<span class="page-cur">' . $i . '</span>';
        } else {
            $html.='<a rel="nofollow" href="javascript:void(0)" onclick="GetManageProductList(' . $i . ')">' . $i . '</a>';
        }
    }
    if ($p >= $total_page) {
        $html.='<span class="page-prev">Next</span>';
    } else {
        $next = $p + 1;
        $html.='<a rel="nofollow" class="page-next" href="javascript:void(0)" onclick="GetManageProductList(' . $next . ')" >Next</a>';
    }
    return $html;
}
